package org.cklxh.widget;

import java.util.Calendar;

import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.AttributeSet;

/**
 * 自定义的DigitalClock时间输出
 * 新增:
 * 1:自定义格式
 * 2:自定义时间
 * 
 * @author cklxh
 * 
 */
public class DigitalClock extends android.widget.DigitalClock {

	private long deviation_time_millis;//矫正时间变量
	
	Calendar mCalendar;
	private final static String m12 = "yyyy-MM-dd hh:mm:ss aa";// h:mm:ss aa
	private final static String m24 = "yyyy-MM-dd kk:mm:ss";// k:mm:ss
	private FormatChangeObserver mFormatChangeObserver;

	private Runnable mTicker;
	private Handler mHandler;

	private boolean mTickerStopped = false;

	String mFormat;

	public DigitalClock(Context context) {
		super(context);
		initClock(context);
	}

	public DigitalClock(Context context, AttributeSet attrs) {
		super(context, attrs);
		initClock(context);
	}

	private void initClock(Context context) {
		//Resources r = context.getResources();

		if (mCalendar == null) {
			mCalendar = Calendar.getInstance();
		}

		mFormatChangeObserver = new FormatChangeObserver();
		getContext().getContentResolver().registerContentObserver(
				Settings.System.CONTENT_URI, true, mFormatChangeObserver);

		setFormat();
	}

	@Override
	protected void onAttachedToWindow() {
		mTickerStopped = false;
		super.onAttachedToWindow();
		mHandler = new Handler();

		/**
		 * requests a tick on the next hard-second boundary
		 */
		mTicker = new Runnable() {
			public void run() {
				if (mTickerStopped)
					return;
				mCalendar.setTimeInMillis(System.currentTimeMillis()+deviation_time_millis);
				setText(DateFormat.format(mFormat, mCalendar));
				invalidate();
				long now = SystemClock.uptimeMillis();
				long next = now + (1000 - now % 1000);
				mHandler.postAtTime(mTicker, next);
			}
		};
		mTicker.run();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mTickerStopped = true;
	}

	/**
	 * Pulls 12/24 mode from system settings
	 */
	private boolean get24HourMode() {
		return DateFormat.is24HourFormat(getContext());
	}

	private void setFormat() {
		if (get24HourMode()) {
			mFormat = m24;
		} else {
			mFormat = m12;
		}
	}

	private class FormatChangeObserver extends ContentObserver {
		public FormatChangeObserver() {
			super(new Handler());
		}

		@Override
		public void onChange(boolean selfChange) {
			setFormat();
		}
	}
	
	//设置显示的时间
	public void setTime(long millis){
		deviation_time_millis=millis-System.currentTimeMillis();
	}
	
	//得到时间
	public long getTime(){
		return deviation_time_millis+System.currentTimeMillis();
	}

	public long getDeviation_time_millis() {
		return deviation_time_millis;
	}

	public void setDeviation_time_millis(long deviation_time_millis) {
		this.deviation_time_millis = deviation_time_millis;
	}

	
}
